<?php

class IndexController extends Zend_Controller_Action
{
		
	public function init()    
    {       
        
    }
	public function NotLogin() {
		$auth = Zend_Auth::getInstance();
		if (!$auth->hasIdentity()) {
			if ($this->_request->getActionName() != 'login') {
				$this->_redirect('/account/login');
			}
		}
	}
    public function indexAction()
    {
       $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity())
        { 
          $infoUser = $auth->getIdentity();
          $customer_id = $infoUser['user_id'];
          $sql = "select * from customer where customer_id = '$customer_id'";
          $connect = new Application_Model_ConnectDB();
          $data = $connect->dbTable($sql);
          $this->view->auth = $data[0]['customer_name'];       
        }
        else 
        	$this->view->auth = "";        			
    }
    
	
    public function thucdonAction()
    {
    	$auth = Zend_Auth::getInstance(); 
    	$bill = array(); // bill co 3 thanh phan la tong tien, tong mon, tong gia vi
    	$info = $auth->getIdentity();
    	$order_id = $info['order_id'];
    	$connect = new Application_Model_ConnectDB();  
    	
    	$sql = "select * from items"; //cau truy van lay toan bo mon an
    	
    	$query1 = "select * from order_item, items 
    			   where order_item.item_id = items.item_id and order_item.order_id = '$order_id'";   
		//cau truy van nhung mon ma nguoi dung da dat
    	
        $total_item = 0; $total_money = 0; $bill['total_item'] =0;
        $res1 = $connect->dbTable($query1);
        
        if(!count($res1))
        	$this->view->total_item = 0;       
        else {
        	for($i = 0;$i < count($res1);$i++)
        	{
        		$total_item += $res1[$i]['quantity'];
        		$total_money += $res1[$i]['quantity'] * $res1[$i]['price'];
        	}
        	$bill['total_item'] = $total_item;
        }
        
        $query2 = "select * from order_spice, spices
        		   where order_spice.spice_id = spices.spice_id and order_id = '$order_id'";
        // cau truy van de lay nhung phu gia nguoi dung da chon
        
     	$total_spice = 0; $bill['total_spice'] =0;
     	$res2 = $connect->dbTable($query2);
        
     	if(!count($res2))
        	$this->view->total_spice = 0;       
        else {
        	for($i = 0;$i < count($res2);$i++)
        	{
        		$total_spice += $res2[$i]['quantity'];
        		$total_money += $res2[$i]['quantity'] * $res2[$i]['price'];
        	}
        	$bill['total_spice'] = $total_spice;
        }
        
        $bill['total_money'] = $total_money;                	
    	$data = $connect->dbTable($sql);
    	//print out here!!!
    	$this->view->data = $data;
    	$this->view->bill = $bill; 
    }

    public function giaohangAction()
    {
    	$auth = Zend_Auth::getInstance(); 
    	$info = $auth->getIdentity(); 
    	$order_id = $info['order_id']; 
    	$customer_id = $info['user_id']; 
    	$sql = "SELECT * FROM `orders` WHERE customer_id='$customer_id'";
    	$connect = new Application_Model_ConnectDB();
    	$res= $connect->dbTable($sql);
    	if (count($res)){
    		$this->view->giao =0;
    	}
    	else {
    		$this->view->giao=1;
    		$this->view->data=$res;
    	}
    	
    }

    public function lienheAction()
    {
        
    }
    
 	public function accountAction()
    {
        $auth = Zend_Auth::getInstance(); 
    	$info = $auth->getIdentity(); 
    	$order_id = $info['order_id']; $customer_id = $info['user_id'];    	
    	$connect = new Application_Model_ConnectDB();
    	$sql1 = "select * from order_item, items 
    			where order_item.item_id = items.item_id and order_item.order_id = '$order_id'";
    	$res1 = $connect->dbTable($sql1); $item_money = 0; $item_quantity = 0;
    	for($i = 0;$i < count($res1);$i++)
    	{
    		$item_quantity += $res1[$i]['quantity'];
    		$item_money = $item_money + ($item_quantity * $res1[$i]['price']);
    	}    	
    	$sql2 = "select * from order_spice, spices 
    			where order_spice.spice_id = spices.spice_id and order_spice.order_id = '$order_id'";
    	$res2 = $connect->dbTable($sql2); $spice_money = 0; $spice_quantity = 0;
    	for($j = 0;$j < count($res2);$j++)
    	{
    		$spice_quantity += $res2[$j]['quantity'];
    		$spice_money = $spice_money + ($spice_quantity * $res2[$j]['price']);
    	}    	
    	$total_money = $item_money + $spice_money;
    	$sql3 = "select * from bill"; $res3 = $connect->dbTable($sql3);
    	$numRows = count($res3) + 1;
    	if($numRows > 0 && $numRows <= 9)
    		$bill_id = "HD00" .$numRows;
    	else if($numRows > 9 && $numRows <= 99)
    		$bill_id = "HD0" .$numRows ;
    	else 
    		$bill_id = "HD" .$numRows;   	   		
    	$dataInfo = array('bill_id' => $bill_id,
    				  	  'customer_id' => $customer_id,
    				      'item_quantity' => $item_quantity,
    				      'item_money' => $item_money,
    				      'spice_quantity' => $spice_quantity,
    				      'spice_money' => $spice_money,
    				      'total_money' => $total_money);    	
    	$connect->dbConnect()->insert('bill', $dataInfo); 
    	$where = "order_id = '$order_id'";       			
        $connect->dbConnect()->delete('order_item',$where);
        $connect->dbConnect()->delete('order_spice',$where);
        $connect->dbConnect()->delete('orders',$where);       
        $this->_redirect('/index/giaohang'); 
    	
    }
    
	public function thanhtoanAction()
    {
    	$auth = Zend_Auth::getInstance(); 
    	$info = $auth->getIdentity(); $order_id = $info['order_id'];
        $sql1 = "select * from order_item, items
        		where order_item.item_id = items.item_id and order_id = '$order_id'";
        $sql2 = "select * from order_spice, spices
        		where order_spice.spice_id = spices.spice_id and order_id = '$order_id'"; 
        $connect = new Application_Model_ConnectDB();
        $listItem = $connect->dbTable($sql1);
        $listSpice = $connect->dbTable($sql2);
        $this->view->items = $listItem;
        $this->view->spices = $listSpice;        
    }

    public function giaAction()
    {
    	$this->NotLogin();
    	$item_id = $this->_request->getParam('item_id');    	
        $sql = "select * from eatwith th, spices sp
        		where th.spice_id = sp.spice_id and th.item_id = '$item_id'";
        $connect = new Application_Model_ConnectDB();  	
        $data = $connect->dbTable($sql);
    	$this->view->data = $data; 
    }
    
    public function itemAction()
    {
		$this->NotLogin();
    	 $item_id = $this->_request->getParam('item_id');
         $date = date('Y-m-d H:i:s');
         $auth = Zend_Auth::getInstance(); $info = $auth->getIdentity();
         $order_id = $info['order_id']; $customer_id = $info['user_id'];
         $sql = "select * from orders where order_id = '$order_id'";
         $connect = new Application_Model_ConnectDB();       
         if(!count($connect->dbTable($sql)))
         {
         	$data = array('order_id' => $order_id,
        			      'customer_id' => $customer_id,        				  
        			      'order_date' => $date,
         			      'delivery_date' => $date);
         	$connect->dbConnect()->insert('orders',$data);
         }
         $query = "select * from order_item where order_id = '$order_id' and item_id = '$item_id'";         
         if(!count($connect->dbTable($query)))
         {        	
         	$record = array('order_id' => $order_id,
         					'item_id' => $item_id,
         					'quantity' => 1);
         	$connect->dbConnect()->insert('order_item', $record);      	
         }
         else 
         {
         	$res = $connect->dbTable($query);
         	$amount = $res[0]['quantity'] + 1;
         	$where = "order_id = '$order_id' and item_id = '$item_id'";
         	$modify = array('order_id' => $order_id,
         					'item_id' => $item_id,
         					'quantity' => $amount);
         	$connect->dbConnect()->update('order_item', $modify,$where);        	
         }   
         $this->_redirect('index/thucdon');                             	
    }

 	public function addAction()
    {
    	$auth = Zend_Auth::getInstance(); 
    	$info = $auth->getIdentity(); $order_id = $info['order_id'];
    	$item_id = $this->_request->getParam('item_id');
    	$spice_id = $this->_request->getParam('spice_id');    	
    	$connect = new Application_Model_ConnectDB();       
    	$sql = "select * from order_spice where order_id = '$order_id' and spice_id = '$spice_id'";    	  
        if(!count($connect->dbTable($sql)))
         {        	
         	$record = array('order_id' => $order_id,
         					'spice_id' => $spice_id,
         					'quantity' => 1);
         	$connect->dbConnect()->insert('order_spice', $record);      	
         }
         else 
         {
         	$res = $connect->dbTable($sql);
         	$amount = $res[0]['quantity'] + 1;
         	$where = "order_id = '$order_id' and spice_id = '$spice_id'";
         	$modify = array('order_id' => $order_id,
         					'spice_id' => $spice_id,
         					'quantity' => $amount);
         	$connect->dbConnect()->update('order_spice', $modify,$where);        	
         }
         $url = "index/gia?item_id=" .$item_id;           
         $this->_redirect($url);
    }

    public function deleteAction()
    {
    	$auth = Zend_Auth::getInstance(); 
    	$info = $auth->getIdentity(); $order_id = $info['order_id']; 
    	$connect = new Application_Model_ConnectDB();
    	if($this->_request->getParam('delete_item') == 'yes')
    	{
    		
    		$item_id = $this->_request->getParam('item_id');
    		$sql = "select * from order_item where order_id = '$order_id' and item_id = '$item_id'";
    		$where = "order_id = '$order_id' and item_id = '$item_id'";    		 
    		$data = $connect->dbTable($sql);
    		$quantity = $data[0]['quantity'] - 1;
    		if($quantity <= 0)
    		{
    			$where = "order_id='$order_id' and item_id = '$item_id'";       			
       			$connect->dbConnect()->delete('order_item',$where);
    		} else 
    		{
	    		$modifyInfo = array('order_id' => $order_id,
	    							'item_id' => $item_id,
	    							'quantity' => $quantity);
	    		$connect->dbConnect()->update('order_item', $modifyInfo,$where);
    		}
    		$this->_redirect('/index/thanhtoan');
    	}

    	if($this->_request->getParam('delete_spice') == 'yes')
    	{
    		$spice_id = $this->_request->getParam('spice_id');
    		$sql = "select * from order_spice where order_id = '$order_id' and spice_id = '$spice_id'";
    		$where = "order_id = '$order_id' and spice_id = '$spice_id'";    		 
    		$data = $connect->dbTable($sql);
    		$quantity = $data[0]['quantity'] - 1;
    		if($quantity <= 0)
    		{
    			$where = "order_id='$order_id' and spice_id = '$spice_id'";       			
       			$connect->dbConnect()->delete('order_spice',$where);
    		} else {  		
    			$modifyInfo = array('order_id' => $order_id,
    							'spice_id' => $spice_id,
    							'quantity' => $quantity);
    			$connect->dbConnect()->update('order_spice', $modifyInfo,$where);
    		}
    		$this->_redirect('/index/thanhtoan');
    	}
    }
}